﻿using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using WebAppDbTest.Models;
using WebAppDbTest.Services;

namespace WebAppDbTest.Controllers;

[ApiController]
[Route("api/[controller]")]
public class SqliteController : ControllerBase
{
    #region 构造函数 DI 
    private readonly IAppLogsServices _logger;
    private readonly IFreeSql _freeSql;
    private readonly IWeatherForecastServices _weatherForecastServices;

    public SqliteController(IAppLogsServices logger,
        IFreeSql freeSql,
        IWeatherForecastServices weatherForecastServices)
    {
        _logger = logger;
        _freeSql = freeSql;
        _weatherForecastServices = weatherForecastServices;
    }
    #endregion

    #region About SQLite CRUD
    /// <summary>
    /// 单条数据添加
    /// </summary>
    /// <returns></returns>
    [HttpPost("AddSingle")]
    public async Task<Guid> AddSingleAsync()
    {
        // 模拟数据
        var wf = _weatherForecastServices.GetWeatherForecasts(1).First();
        return await _weatherForecastServices.SqliteAddSingleAsync(wf);
    }

    /// <summary>
    /// 批量数据添加
    /// </summary>
    /// <param name="count"></param>
    /// <returns></returns>
    [HttpPost("AddBulk")]
    public async Task<int> AddBulkAsync(int count)
    {
        // 模拟数据
        var wfs = _weatherForecastServices.GetWeatherForecasts(count);
        return await _weatherForecastServices.SqliteAddBulkAsync(wfs);
    }

    /// <summary>
    /// 单条查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("GetSingle")]
    public async Task<WeatherForecast> GetSingleAsync(Guid id)
    {
        return await _weatherForecastServices.SqliteGetSingleAsync<WeatherForecast>(id);
    }

    /// <summary>
    /// 批量查询
    /// </summary>
    /// <returns></returns>
    [HttpGet("GetAll")]
    public async Task<IEnumerable<Guid>> GetAllAsync()
    {
        var wfs = await _weatherForecastServices.SqliteGetAllAsync<WeatherForecast>();

        var guids = new List<Guid>();
        foreach (var item in wfs)
        {
            guids.Add(item.Id);
        }

        return guids;
    }

    /// <summary>
    /// 修改单条
    /// </summary>
    /// <returns></returns>
    [HttpPut("UpdateSingle")]
    public async Task<bool> UpdateSingleAsync([FromBody] Guid id)
    {
        // 模拟数据
        var wf = _weatherForecastServices.GetWeatherForecasts(1).First();
        wf.Id = id;

        return await _weatherForecastServices.SqliteUpdateSingleAsync(wf);
    }

    /// <summary>
    /// 修改多条
    /// </summary>
    /// <returns></returns>
    [HttpPut("UpdateBulk")]
    public async Task<int> UpdateBulkAsync(List<Guid> ids)
    {
        var wfs = _weatherForecastServices.GetWeatherForecasts(ids.Count).ToList();
        for (int i = 0; i < ids.Count; i++)
        {
            wfs[i].Id = ids[i];
        }

        return await _weatherForecastServices.SqliteUpdateBulkAsync(wfs);
    }

    /// <summary>
    /// 删除单条
    /// </summary>
    /// <returns></returns>
    [HttpDelete("DeleteSingle")]
    public async Task<bool> DeleteSingleAsync(Guid id)
    {
        return await _weatherForecastServices.SqliteDeleteSingleAsync<WeatherForecast>(id);
    }

    /// <summary>
    /// 批量删除
    /// </summary>
    /// <returns></returns>
    [HttpDelete("DeleteBulk")]
    public async Task<int> DeleteBulkAsync(List<Guid> ids)
    {
        return await _weatherForecastServices.SqliteDeleteBulkAsync<WeatherForecast>(ids);
    }
    #endregion

}
